﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DAL.Entities;

namespace DAL.EntitesDAL
{
    public class KhoahocDAL : BaseDAL, UTL.IBaseDAL
    {
        #region Implement

        public int Count()
        {
            throw new NotImplementedException();
        }

        public DataTable Select()
        {
            return ExecuteQuery("sp_SelectAll_Khoahoc");
        }

        public DataTable Select(string id)
        {
            string sql = @"Select * From tb_Khoahoc Where Makhoa = {0} ";
            sql = string.Format(sql, id);
            return ExecuteQuery(sql);
        }

        public DataTable Select_Date(string makhoa) 
        {
            string sql = @"select Ngaykhaigiang,Ngayketthuc from tb_Khoahoc where Makhoa='{0}' ";
            sql = string.Format(sql, makhoa);
            return ExecuteQuery(sql); 
        }

        public DataTable Select(object obj)
        {
            throw new NotImplementedException();
        }

        public object GetByKey(object key)
        {
            throw new NotImplementedException();
        }

        public bool Delete(string id)
        {
            var sql = "sp_Delete_Khoahoc {0}";
            sql = string.Format(sql, id);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Insert(object obj)
        {
            var o = (Khoahoc)obj;
            var sql = "set dateformat dmy; execute sp_Insert_Khoahoc '{0}', N'{1}', '{2}', '{3}' ";
            sql = string.Format(sql, o.Makhoa, o.Tenkhoa, o.Ngaykhaigiang, o.Ngayketthuc);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public bool Update(object obj)
        {
            var o = (Khoahoc)obj;
            var sql = "set dateformat dmy; execute sp_Update_Khoahoc '{0}', N'{1}', '{2}', '{3}' ";
            sql = string.Format(sql, o.Makhoa, o.Tenkhoa, o.Ngaykhaigiang, o.Ngayketthuc);
            return ExecuteNonQuery(sql) > 0 ? true : false;
        }

        public DataTable Search(string name)
        {
            var sql = @"select * from tb_Khoahoc where Tenkhoa like N'%{0}%' ";
            sql = string.Format(sql, name);
            return ExecuteQuery(sql);
        }

        public DataTable Search(DAL.Entities.Khoahoc o)
        {
            string sql;
            string tenkhoa = o.Tenkhoa;

            if (o.Makhoa != "")
                sql = @"Select * From tb_Khoahoc Where Makhoa like '%"+o.Makhoa+"%' and Tenkhoa like N'%"+tenkhoa+"%' ";
            else
                return Search(tenkhoa);

            return ExecuteQuery(sql);
        }

       
        #endregion

        #region Other

        public DataTable Select_Max()
        {
            var sql = @"Select Max(Makhoa) as Ma From tb_Khoahoc";
            return ExecuteQuery(sql);
        }

        public DataTable Select_Current(DateTime dt)
        {
            string sql = @"Set Dateformat dmy; Select Makhoa,Tenkhoa From tb_Khoahoc Where Ngaykhaigiang <= '{0}' and Ngayketthuc >= '{1}' ";
            sql = string.Format(sql, dt, dt);
            return ExecuteQuery(sql);
        }

        #endregion
    }
}
